﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Renci.SshNet;
using Renci.SshNet.Sftp;
using LiteLib;
 
namespace XpressTelephoneListener
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Settings.GetValue(this);
        }

       
        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            AboutForm form = new AboutForm();
            form.ShowDialog();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowSettings(-1);
        }
        void ShowSettings(int nSelectedIndex)
        {
            SettingsForm form = new SettingsForm();
            form.SelectedIndex = nSelectedIndex;
            if (form.ShowDialog(this) == DialogResult.OK)
            {
                //ThreadCount = Settings.GetValue("Threads count", 10);
                InitValues();
            }
        }
        void InitValues()
        {
            //WebDepth = Settings.GetValue("Web depth", 3);
            //RequestTimeout = Settings.GetValue("Request timeout", 20);
            //SleepFetchTime = Settings.GetValue("Sleep fetch time", 2);
            //SleepConnectTime = Settings.GetValue("Sleep connect time", 1);
            //KeepSameServer = Settings.GetValue("Keep same URL server", false);
            //AllMIMETypes = Settings.GetValue("Allow all MIME types", true);
            //KeepAlive = Settings.GetValue("Keep connection alive", true);
            //ExcludeHosts = Settings.GetValue("Exclude Hosts", ".org; .gov;").Replace("*", "").ToLower().Split(';');
            //ExcludeWords = Settings.GetValue("Exclude words", "").Split(';');
            //ExcludeFiles = Settings.GetValue("Exclude files", "").Replace("*", "").ToLower().Split(';');
            //LastRequestCount = Settings.GetValue("View last requests count", 20);
            //Downloadfolder = Settings.GetValue("Download folder", System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal));
            //MIMETypes = GetMIMETypes();
            //TextEncoding = GetTextEncoding();
        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuItemSettings_Click(object sender, EventArgs e)
        {
            ShowSettings(-1);
        }

        private void menuItemConnections_Click(object sender, EventArgs e)
        {
            ShowSettings(1);
        }

        private void menuItemOutput_Click(object sender, EventArgs e)
        {
            ShowSettings(0);
        }

        private void menuItemAbout_Click(object sender, EventArgs e)
        {
            AboutForm form = new AboutForm();
            form.ShowDialog();
        }

        private void buttonGo_Click(object sender, EventArgs e)
        {
            // get all file if the main filelist is not empty 
            //  get search values from textboxes  

            // start date  
            // end date  
            // type  
            // Telephone extenion  
          


             using (var client = new SftpClient(Settings.GetValue("ServerIP"), Settings.GetValue("UserName"), Settings.GetValue("Password")))
            {
                client.Connect();
                IEnumerable<SftpFile> listfiles = client.ListDirectory(Settings.GetValue("FolderPath"), null);
                

                foreach (SftpFile item in listfiles)
                {
                    if (item.FullName != null)
                    {
                        
                        	
			
			try
			{	// update thread information in the threads view list
                //itemLog = this.listViewThreads.Items[item.i];
                //int nDepth = uri.Depth;\
                ListViewItem itemLog = new ListViewItem(item.Name);
                itemLog.SubItems.Add(new ListViewItem.ListViewSubItem(itemLog, item.Name));
                itemLog.SubItems.Add(new ListViewItem.ListViewSubItem(itemLog, item.IsDirectory.ToString()));
                itemLog.SubItems.Add(new ListViewItem.ListViewSubItem(itemLog, item.FullName));
              //  itemLog.SubItems.Add(new ListViewItem.ListViewSubItem(itemLog, "sub item"));
               // itemLog.SubItems.Add(new ListViewItem.ListViewSubItem(itemLog, "sub item"));
              //  itemLog.SubItems.Add(new ListViewItem.ListViewSubItem(itemLog, "sub item"));
              //  itemLog.SubItems.Add(new ListViewItem.ListViewSubItem(itemLog, "sub item"));

                this.listViewThreads.Items.Add(itemLog);
                //itemLog.SubItems[1].Text =  item.Name;
                //itemLog.ImageIndex = 1;
                //itemLog.BackColor = Color.WhiteSmoke;
                //// initialize status to Connect
                //itemLog.SubItems[2].Text = "Connect";
                //itemLog.ForeColor = Color.Red;
                //itemLog.SubItems[3].Text = item.FullName;
                //itemLog.SubItems[4].Text = "";
                //itemLog.SubItems[5].Text = "";
			}
			catch(Exception)
			{
			}
                       

                    }
                }
                client.Disconnect();
            }
        }
    }
}
